<template>
  <div class="group-chat-page">
    <div class="chat-page-header">坐忘道（108）</div>
    <div class="chat-page-message-container no-drag" ref="messageContainerRef">
      <template v-for="(item, index) in messageList" :key="index">
        <div
          v-if="index == 0 || item.time - messageList[index - 1].time > 180000"
          class="time-item flex-center"
        >
          <div class="content">
            {{ formatTime(item.time) }}
          </div>
        </div>
        <div :class="['message-item', 'clearfix', item.id == -1 ? 'me' : 'other']">
          <img v-if="item.id != -1" :src="item.avatar" alt="" class="avatar" />
          <div class="message-info">
            <div class="name" v-if="item.id != -1">{{ item.name }}</div>
            <div class="content">{{ item.message }}</div>
          </div>
          <img v-if="item.id == -1" :src="item.avatar" alt="" class="avatar" />
        </div>
      </template>
    </div>
    <chatEdit @sendMessage="sendMessage"></chatEdit>
  </div>
</template>

<script setup>
import { ref, nextTick } from 'vue'
import { chatMessageList } from './mock'
import chatEdit from '@/components/chatEdit/index.vue'
import dayjs from 'dayjs'

const messageContainerRef = ref(null)
const messageList = ref([])

const initMessage = () => {
  Promise.resolve(JSON.parse(JSON.stringify(chatMessageList))).then((res) => {
    messageList.value = res
    scrollToBottom()
  })
}
initMessage()

const sendMessage = (message) => {
  messageList.value.push(message)
  scrollToBottom()
  console.log('🚀 ~ sendMessage ~ message:', message)
}

const scrollToBottom = () => {
  nextTick(() => {
    messageContainerRef.value.scrollTop =
      messageContainerRef.value.scrollHeight - messageContainerRef.value.clientHeight + 10
  })
}

const formatTime = (time) => {
  const day = dayjs(time)
  const now = dayjs().format('YYYY-MM-DD')

  if (day.isSame(now, 'day')) {
    return day.format('HH:mm')
  } else if (day.add(1, 'day').isSame(now, 'day')) {
    return `昨天 ${day.format('HH:mm')}`
  } else if (day.isSame(now, 'week')) {
    return `${['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'][day.day()]} ${day.format('HH:mm')}`
  } else {
    return day.format('YYYY-MM-DD HH:mm')
  }
}
</script>
<style lang="less" scoped>
.group-chat-page {
  display: flex;
  flex-direction: column;
  height: 100%;
  .chat-page-header {
    flex-shrink: 0;
    display: flex;
    align-items: center;
    height: 65px;
    padding-left: 20px;
    font-size: 18px;
  }
  .chat-page-message-container {
    flex: 1;
    overflow-y: auto;
    border-top: 1px solid #e1e1e1;
    border-bottom: 1px solid #e1e1e1;
    padding: 0 30px;
    & > div {
      margin-bottom: 10px;
      &:first-child {
        margin-top: 10px;
      }
    }
    .message-item {
      display: flex;
      .avatar {
        height: 35px;
        width: 35px;
        border-radius: 2px;
        cursor: pointer;
      }
      .message-info {
        .name {
          color: #b0b0b0;
          font-size: 12px;
          margin-bottom: 5px;
          user-select: none;
        }
        .content {
          position: relative;
          background-color: #fff;
          font-size: 14px;
          border-radius: 5px;
          padding: 8px 8px;
          &::before {
            position: absolute;
            top: 11px;
            content: '';
            width: 0;
            height: 0;
            border: 7px solid transparent;
          }
        }
      }
      &.other {
        .message-info {
          margin-left: 8px;
          .content {
            &::before {
              left: -12px;
              border-right-color: #fff;
            }
          }
        }
      }
      &.me {
        justify-content: flex-end;
        .message-info {
          margin-right: 8px;
          .name {
            text-align: right;
          }
          .content {
            background-color: #95ec69;
            &::before {
              right: -12px;
              border-left-color: #95ec69;
            }
          }
        }
      }
    }
    .time-item {
      user-select: none;
      .content {
        background-color: #dadada;
        color: #fff;
        font-size: 12px;
        padding: 2px 5px;
        border-radius: 3px;
      }
    }
  }
  .chat-page-editor-container {
    height: 200px;
  }
}
</style>
